package com.stagesmsserver.smsserver.service.impl;

//import com.aliyuncs.DefaultAcsClient;
//import com.aliyuncs.IAcsClient;
//import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
//import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
//import com.aliyuncs.exceptions.ClientException;
//import com.aliyuncs.profile.DefaultProfile;
//import com.aliyuncs.profile.IClientProfile;
import com.netflix.client.ClientException;
import com.stagecommon.common.config.ThreadPoolConfiguration;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.dto.SmsCodeReq;
import com.stagesmsserver.smsserver.service.SmsSendCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @description：短信发送验证码
 * @author： SAYE
 * @create： 2024/4/20 20:43
 */
@Slf4j
@Service
public class SmsSendCodeServiceImpl implements SmsSendCodeService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ThreadPoolConfiguration threadPoolConfiguration;

    @Override
    public CompletableFuture<RespBean<Object>> sendCode(SmsCodeReq smsCodeReq)  {
        return send(smsCodeReq);
    }

    @Override
    public RespBean<String> verifyCode(SmsCodeReq smsCodeReq) {
        return Objects.equals(redisTemplate.opsForValue().get("sms:ver:code" + smsCodeReq.getPhone()), smsCodeReq.getCode())
                ? RespBean.operationSuccess("校验成功",200, "ok")
                : RespBean.operationSuccess("验证码错误",-10, "error");
    }

    private CompletableFuture<RespBean<String>> sms(SmsCodeReq smsCodeReq) {
        CompletableFuture<RespBean<String>> resultMessageCompletableFuture =
                CompletableFuture.supplyAsync(() ->
                                RespBean.operationSuccess("验证码发送成功",200,  "success"),
                        threadPoolConfiguration.threadPoolTaskExecutor());
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            try {
                sendSms(smsCodeReq);
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }, threadPoolConfiguration.threadPoolTaskExecutor());
        return resultMessageCompletableFuture;
    }

    private CompletableFuture<RespBean<Object>> send(SmsCodeReq smsCodeReq) {
        Object o = redisTemplate.opsForValue().get("wait:ver:send" + smsCodeReq.getPhone());
        return CompletableFuture.supplyAsync(() -> Objects.equals(null, o)
                ? RespBean.operationSuccess("发送成功",200, sms(smsCodeReq)) :
                RespBean.operationSuccess("请稍等",-10, null), threadPoolConfiguration.threadPoolTaskExecutor());
    }

    private void sendSms(SmsCodeReq req) throws ClientException {
//        String code = String.valueOf(ThreadLocalRandom.current().nextInt(899999) + 100000);
//        IClientProfile profile = DefaultProfile.getProfile("", "", "");
//        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
//        IAcsClient acsClient = new DefaultAcsClient(profile);
//        SendSmsRequest request = new SendSmsRequest();
//        request.setPhoneNumbers(req.getPhone());
//        request.setSignName("   ");
//        request.setTemplateCode("");
//        request.setTemplateParam("{code:" + code + "}");
//        redisTemplate.opsForValue().set("sms:ver:code" + req.getPhone(), code, 5, TimeUnit.MINUTES);
//        redisTemplate.opsForValue().set("wait:ver:send" + req.getPhone(), System.currentTimeMillis(), 1, TimeUnit.MINUTES);
//        SendSmsResponse acsResponse = acsClient.getAcsResponse(request);
//        System.out.println(acsResponse.getMessage());
    }

}

